<?php
namespace App\Services\Log\Log;

use App\Models\Log\Log;

class LogLogic
{
    /**
     * 生成操作日志
     * @return array
     */
    public function addLog($data)
    {
        if ($data['type'] == 1) {
            $action = 'ID.' . $data['id'] . '的用户-' . $data['name'] . '-新增了一条' . $data['record'];
        } elseif ($data['type'] == 2) {
            $action = 'ID.' . $data['id'] . '的用户-' . $data['name'] . '-修改了一条' . $data['record'];
        } elseif ($data['type'] == 3) {
            $action = 'ID.' . $data['id'] . '的用户-' . $data['name'] . '-删除了一条' . $data['record'];
        } elseif ($data['type'] == 4) {
            $action = 'ID.' . $data['id'] . '的用户-' . $data['name'] . '-登陆了系统';
        } elseif ($data['type'] == 5) {
            $action = 'ID.' . $data['id'] . '的用户-' . $data['name'] . '-退出了系统';
        } else {
            $action = 'ID.' . $data['id'] . '的用户-' . $data['name'] . '-系统异常，这是一条非法记录';
        }

        $save_data['action'] = $action;
        $save_data['type'] = $data['type'];
        $save_data['created_at'] = date('Y-m-d H:i:s', time());
        $save_data['updated_at'] = date('Y-m-d H:i:s', time());

        $rst = Log::insertGetId($save_data);

        if ($rst) {
            return auto_return(['id' => $rst], '添加成功！');
        }
        return auto_failure($rst, '日志添加失败！');
    }
}
